/*工具类*/
var CoreUtil = (function () {
    var coreUtil = {};

    /* GET */
    coreUtil.sendGet = function (url, params, ft) {
        this.sendAJAX(url, params, ft, "GET")
    }

    /* POST */
    coreUtil.sendPost = function (url, params, ft, cache, async) {
        this.sendAJAX(url, JSON.stringify(params), ft, "POST", cache, async)
    }
    
    /* PUT */
    coreUtil.sendPut = function (url, params, ft) {
        this.sendAJAX(url, JSON.stringify(params), ft, "PUT")
    }
    /* DELETE */
    coreUtil.sendDelete = function (url, params, ft) {
        this.sendAJAX(url, JSON.stringify(params), ft, "DELETE")
    }


    /* ajax */
    coreUtil.sendAJAX = function (url, params, ft, method, cache, async, time) {
        // add 2021-12-29 13:11:45 其他请求30 * 1000，特殊请求time
    	if(!CoreUtil.isEmpty(time)){
        	time = 30 * 1000;
        };
        
    	$.ajax({
            url: url,
            timeout : time,
            cache: cache,
            async: async,
            data: params,
            type: method,
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            beforeSend : function(request) {
				request.setRequestHeader("authorization", CoreUtil
						.getData("access_token"));
			},
            success: function (res) {
            	ft(res);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            	if (textStatus == 418) {
            		mui.toast("不支持的客户端", "");
            	} else if (textStatus == 419) {
            		mui.toast("未通过人脸验证监控", "");
            	} else if (textStatus == 420) {
            		mui.toast("Token为空，请返回首页重新登录再尝试", "");
            	} else if (textStatus == 421) {
            		mui.toast("缓存服务器连接丢失", "");
            	} else {
            		mui.toast("无法连接服务器，请稍后再试!", "");
            	};
            	
            }
        })
    };
    
    /* 从本地缓存拿数据 */
	coreUtil.getData = function(key) {
        var value = sessionStorage.getItem(key);
        if (CoreUtil.isEmpty(value)) {
            var url = "/index/swnb/login";
            window.location.href = url;
        }
        return value;
        
	};

    //判断字符是否为空的方法
    coreUtil.isEmpty = function (obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }
    
    return coreUtil;
})(CoreUtil, window);
